package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.rescue.entity.RescueInfo;
import com.dykj.fireFighting.api.base.rescue.service.IRescueInfoService;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/monitor")
public class RescueController {

    @DubboReference
    private IRescueInfoService iRescueInfoService;
    @DubboReference
    private IEnterpriseService iEnterpriseService;

    //获取列表数据
    @ApiOperation(value = "获取救援单位列表", notes = "获取救援单位列表")
    @GetMapping("/resure/list")
    public CommonResult<List<RescueInfo>> getList(@RequestParam(required = false) String unitId, @RequestParam(required = false) String name, @RequestParam String type) {

        if (StringUtils.isBlank(unitId)) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            if (loginUserVo.getUserType() != 0 && loginUserVo.getEnterpriseType() == 2) {
                unitId = String.valueOf(loginUserVo.getEnterpriseId());
            }
        }
        List<RescueInfo> list = iRescueInfoService.getList(unitId, Integer.parseInt(type), name);
        return CommonResult.success(list);
    }

    //获取列表数据
    @ApiOperation(value = "获取救援单位信息", notes = "获取救援单位信息")
    @GetMapping("/resure/info/{id}")
    public CommonResult<RescueInfo> getInfo(@PathVariable(value = "id") String id) {
        return CommonResult.success(iRescueInfoService.getById(id));
    }

    @ApiOperation(value = "添加救援单位", notes = "添加救援单位")
    @PostMapping("/resure/add")
    public CommonResult save(@Valid @RequestBody RescueInfo rescueInfo) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        if (loginUserVo.getUserType() != 0 && loginUserVo.getEnterpriseType() == 2) {
            String unitId = String.valueOf(loginUserVo.getEnterpriseId());
            rescueInfo.setUnitId(unitId);
        } else {
            rescueInfo.setUnitId("-1");
        }
        rescueInfo.setCreateTime(new Date());
        rescueInfo.setIsDel(1);
        rescueInfo.setId(UUIDUtils.get32UUID());

        iRescueInfoService.save(rescueInfo);
        return CommonResult.success("success");
    }


    @ApiOperation(value = "修改救援单位", notes = "修改救援单位")
    @PostMapping("/resure/update")
    public CommonResult update(@Valid @RequestBody RescueInfo rescueInfo) {

        rescueInfo.setUpdateTime(new Date());
        rescueInfo.setIsDel(1);

        iRescueInfoService.updateById(rescueInfo);
        return CommonResult.success("success");
    }

    @ApiOperation(value = "删除救援单位", notes = "删除救援单位")
    @GetMapping("resure/delete")
    public CommonResult update(String id) {
        iRescueInfoService.deleteById(id);
        return CommonResult.success("success");
    }

    @ApiOperation(value = "获取监控中心下的社会单位信息", notes = "获取监控中心下的社会单位信息")
    @GetMapping("/getSocialList")
    public CommonResult getSocialList(String id) {
        if (StringUtils.isBlank(id)) {
            id = String.valueOf(ShiroUtil.getUser().getEnterpriseId());
        }
        return CommonResult.success(iEnterpriseService.queryUnitSelect(id), "success");
    }


}
